﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICTBilling;

public partial class WebForms_Location : System.Web.UI.Page
{
    private static List<ExceptionHandler> ExecutionCollection = new List<ExceptionHandler>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillLocationTree();
        }
    }
    private void FillLocationTree()
    {
        LocationTreeView.Nodes.Clear();
        List<TreeNode> childs = GetLocationChilds(-1);
        foreach (TreeNode child in childs)
            LocationTreeView.Nodes.Add(child);
    }
    private List<TreeNode> GetLocationChilds(int LocationID)
    {
        List<TreeNode> nodes = new List<TreeNode>();
        ICTBillingEntities billingEntities = new ICTBillingEntities();
        List<tbLocation> locations = billingEntities.tbLocations.Where(location => location.FkParentID == LocationID).ToList();
        for (int index = 0; index < locations.Count(); index++)
        {
            TreeNode node = new TreeNode();
            node.Value = locations[index].PkLocationID.ToString();
            node.Text = locations[index].LocationName;
            if (node.Expanded == null)
                node.Expanded = false;
            node.SelectAction = TreeNodeSelectAction.SelectExpand;
            List<TreeNode> childs = GetLocationChilds(Convert.ToInt32(node.Value));
            foreach (TreeNode child in childs)
                node.ChildNodes.Add(child);
            node.Expand();
            nodes.Add(node);
        }
        return nodes;
    }
    protected void ClearSelectionLinkButton_Click(object sender, EventArgs e)
    {
        if (LocationTreeView.SelectedNode != null)
            LocationTreeView.SelectedNode.Selected = false;
        LocationFormView.ChangeMode(FormViewMode.Insert);
    }
    protected void ParentLabel_PreRender(object sender, EventArgs e)
    {
        ((Label)sender).Text = string.Format("ایجاد موقعیت زیر مجموعه {0}...", (LocationTreeView.SelectedNode == null ? "سطح اول" : LocationTreeView.SelectedNode.Text));
    }
    protected void AddChildLinkButton_PreRender(object sender, EventArgs e)
    {
        if (LocationTreeView.SelectedNode != null)
            ((LinkButton)LocationFormView.FindControl("AddChildLinkButton")).Visible = LocationTreeView.SelectedNode.Depth >= 3 ? false : true;
    }
    protected void LocationFormView_ItemDeleted(object sender, FormViewDeletedEventArgs e)
    {
        LocationFormView.ChangeMode(FormViewMode.Insert);
    }
    protected void LocationTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        LocationFormView.ChangeMode(FormViewMode.Edit);
    }
    protected void AddChildLinkButton_Click(object sender, EventArgs e)
    {
        LocationFormView.ChangeMode(FormViewMode.Insert);
    }
    protected void LocationObjectDataSource_Command(object sender, ObjectDataSourceStatusEventArgs e)
    {
        tbLocation newsItem = e.ReturnValue as tbLocation;
        if (e.ReturnValue != null)
            ExecutionCollection.AddRange(newsItem.Validate(LocationFormView.CurrentMode == FormViewMode.Edit ? ChangeAction.Update : ChangeAction.Insert));

        FillLocationTree();
        LocationFormView.ChangeMode(FormViewMode.Insert);
        LocationTreeView.DataBind();
    }
    protected void InsertLinkButton_Click(object sender, EventArgs e)
    {
        ParentIDHiddenField.Value = ((HiddenField)LocationFormView.FindControl("ParentIDHiddenField")).Value = LocationTreeView.SelectedNode == null ? "-1" : LocationTreeView.SelectedValue;
        TypeHiddenField.Value = ((HiddenField)LocationFormView.FindControl("TypeHiddenField")).Value = (LocationTreeView.SelectedNode == null ? 1 : LocationTreeView.SelectedNode.Depth + 1).ToString();
    }
    protected void UpdateLinkButton_Click(object sender, EventArgs e)
    {
        TypeHiddenField.Value = ((HiddenField)LocationFormView.FindControl("TypeHiddenField")).Value = (LocationTreeView.SelectedNode == null ? 1 : LocationTreeView.SelectedNode.Depth + 1).ToString();
    }
}
